/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   binNode_imple.h                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: TYTY000 <767280080@qq.com>                 +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/04/23 19:14:30 by TYTY000           #+#    #+#             */
/*   Updated: 2024/04/25 19:02:36 by TYTY000          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef __BINNODE_IMPLE__
#define __BINNODE_IMPLE__

#define IsRoot(x) (!((x).parent))
#define IsLChild(x) (!IsRoot(x) && (&(x) == (x).parent->lc))
#define IsRChild(x) (!IsRoot(x) && (&(x) == (x).parent->rc))
#define HasParent(x) (!IsRoot(x))
#define HasLChild(x) ((x).lc)
#define HasRChild(x) ((x).rc)
#define HasChild(x) (HasLChild(x) || HasRChild(x)) // 至少拥有一个孩子
#define HasBothChild(x) (HasLChild(x) && HasRChild(x)) // 同时拥有两个孩子
#define IsLeaf(x) (!HasChild(x))

#define sibling(p)                                                             \
  (IsLChild(**(p)) ? (p)->parent->rc : (p)->parent->lc) /*兄弟*/
#define uncle(x) (sibling((x)->parent))                 /*叔叔*/
#define trace(x) /*来自父亲的引用*/                                     \
  (IsRoot(x) ? _root : (IsLChild(x) ? (x).parent->lc : (x).parent->rc))

template <typename T> Rank BinNode<T>::size() {
  Rank s = 1;
  if (lc)
    s += lc->size();
  if (rc)
    s += rc->size();
  return s;
}

template <typename T> BinNodePosi<T> BinNode<T>::insertAsLC(const T &e) {
  return lc = new BinNode(e, this);
}

template <typename T> BinNodePosi<T> BinNode<T>::insertAsRC(const T &e) {
  return rc = new BinNode(e, this);
}

#include "binNode_succ.h"
#include "binNode_trav.h"

#endif
